import React from 'react';
import {Link, NavLink} from 'react-router-dom';

var $ = (function(){
  var ajax=function(url,callback){
       var xhr=new XMLHttpRequest();
       xhr.open("POST",url,true);
       xhr.send("");
       xhr.onreadystatechange=function(){
         if(xhr.readyState==4&&xhr.status==200){
               callback(JSON.parse(xhr.responseText));
         }
       }
  }
  return {
    http:ajax    
   }

})();


export default class News extends React.Component{  
              constructor(props) {
                super(props);                
            this.state={
              id:{
                id:0
              },
                data:{
                    data:[[{1:''}]]
                }
            }
            }
          //   target(e){
          //    // console.log(e.target.getAttribute('id'));
          //     this.setState({
          //       id:Object.assign({},this.state.id,
          //       {id:e.target.getAttribute('id')}
          //       )
          //   })
          //   this.componentWillMount();
          // }
            componentWillMount(){
              console.log('mount',this.props.match.params.id)
            var _this=this;
            var a=null;
            var arr=[];
           // console.log(this.state.id);
            $.http(`http://10.31.153.45:3000/News/list/${this.props.match.params.id}`,function(data){
              //console.log(data.docs[0].global)
                    arr.push(data.docs)
                  //  console.log(data.docs);
                    _this.setState({
                        data: Object.assign({}, _this.state.data, {data:arr}),
                    })   
            });
            }

    render(){
      let list = [];
      let button=[];
    //  console.log(this.state.data.data[0][0].global);
      //console.log(this.state.data.data[0]);
    const {news} = this.state.data.data[0];
    const newsList = this.state.data.data[0].length;
    for(var i=0;i<newsList;i++){
        const Link='./News';
        // var button=(
        //  <button type="button" class="btn btn-primary" 
        //   data-toggle="button" > <NavLink to={Link}> 返回上一页</NavLink>
        //   </button>
        // )
       //console.log(this.props.match.params.id);
      //  console.log(this.props.match.params.id)
        if(this.state.data.data[0][i]._id===this.props.match.params.id){
            list.push(
                <p>{this.state.data.data[0][i].global}</p>,
                <p>{this.state.data.data[0][i].content}</p>
            )

    }
    }
        return(
          <div>   
               {list}
               {/* {button} */}
               
        </div>
        
        )
    }
  
};

